package com.wwf.util;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/**
 * @author wwf
 * @date 2021年01月07日 16:47:42
 */
public class RpcDecode extends MessageToMessageDecoder<ByteBuf> {
    /**
     * 消息对象类型
     */
    private Class<?> classT;
    /**
     * 序列化类
     */
    private Serializer serializer;
    public RpcDecode(Class classT,Serializer serializer) {
        this.classT = classT;
        this.serializer = serializer;
    }


    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        int dataLength = in.readInt();
        byte[] body = new byte[dataLength];
        in.readBytes(body);
        Object object = serializer.deserialize(classT, body);
        out.add(object);
    }
}
